/**
 * @param {number[]} nums
 * @return {number}
 */
var rob = function (nums) {
  if (nums.length === 0) return 0
  if (nums.length === 1) return nums[0]
  // 假设偷第一家,那么最后一家就不能偷
  let fi = 0
  let fj = nums[0]
  let fk = fj
  for (let i = 1; i < nums.length - 1; i++) {
    fk = Math.max(fi + nums[i], fj)
    fi = fj
    fj = fk
  }
  let max1 = fk
  // 假设不偷第一家，那么最后一家可偷可不偷
  fi = 0
  fj = nums[1]
  fk = fj
  for (let i = 2; i < nums.length; i++) {
    fk = Math.max(fi + nums[i], fj)
    fi = fj
    fj = fk
  }
  return fk > max1 ? fk : max1
};
console.log(rob([2, 3, 2]))